var core    = require("core")
,   db      = core.db
,   schema  = db.schema
,   step    = core.step;

var link = function(example){

    var app = global.app;

    var getUserCount = function(){
        var that = this;
        schema.User.count({}, function(err, count){
            app.user = {
                // count : 1
                count : (err || true) && count
            };
            that.step();
        });
    };

    var getConfig = function(){
        var that = this;
        schema.Config.findOne({}, function(err, config){
            if(config){
                app.config = config;
                return that.step(true);
            }
            else that.step(false);
        });
    };

    var setConfig = function(config){
        var that = this;
        if(!config){
            var config = new schema.Config({});
            config.save(function(err, config){
                if(config)
                    app.config = config;
            });
        }
    };

    step.Step(getUserCount, getConfig, setConfig);

};

module.exports = link;
